<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Wxuser;
use Log;

class WechatController extends Controller
{
    public function index(Request $request)
    {
    	$wechat_user =  session('wechat.oauth_user.default');
    	$data['openid'] = $wechat_user->id;
    	$data['nickname'] = $wechat_user->nickname;
    	$data['headimgurl'] = $wechat_user->avatar;
    	$wxuser = Wxuser::where(['openid'=>$data['openid']])->first();
    	if(!$wxuser) {
    		Wxuser::create($data);
    	}
    	echo '谢谢合作';
    }

    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        Log::info('request arrived.'); # 注意：Log 为 Laravel 组件，所以它记的日志去 Laravel 日志看，而不是 EasyWeChat 日志

        $app = app('wechat.official_account');
        //dd($app);
        $app->server->push(function($message){
         
            //return "欢迎关注!业务问题请加微信 bzs1984 咨询，谢谢。";        
           return env('WELCOME_MSG', '欢迎关注!');
            
        });

        return $app->server->serve();
    }
}
